package org.xiaojl.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.xiaojl.common.Constants;
import org.xiaojl.common.SessionUser;

public class WebUtil extends org.springframework.web.util.WebUtils{

	/**
	 * 添加当前登录用户到session中
	 * 
	 * @param request
	 * @param sessionUser
	 * @return
	 */
	public static SessionUser setSessionUser(HttpServletRequest request,SessionUser sessionUser){
		request.getSession().setAttribute(Constants.SESSION_USER, sessionUser);
		return sessionUser;
	}
	
	/**
	 * 获取当前登录用户
	 * 
	 * @param request
	 * @return
	 */
	public static SessionUser getSessionUser(HttpServletRequest request){
		if(ToolUtil.isNotEmpty(request)){
			return (SessionUser) request.getSession().getAttribute(Constants.SESSION_USER);
		}
		return getSessionUser();
	}
	
	public static SessionUser getSessionUser() {
		//shiro
		return (SessionUser)SecurityUtils.getSubject().getPrincipal();
	}
	
	/**
	 * 读取cookie
	 * @param request
	 * @param key
	 * @return
	 */
	public static String getCookieValue(HttpServletRequest request, String name) {
		Cookie cookie = getCookie(request, name);
		return cookie != null ? cookie.getValue() : null;
	}

	/**
	 * 清除 某个指定的cookie 
	 * @param response
	 * @param key
	 */
	public static void removeCookie(HttpServletResponse response, String key) {
		setCookie(response, key, null, 0);
	}

	/**
	 * 设置cookie
	 * @param response
	 * @param name
	 * @param value
	 * @param maxAgeInSeconds
	 */
	public static void setCookie(HttpServletResponse response, String name, String value, int maxAgeInSeconds) {
		Cookie cookie = new Cookie(name, value);
		cookie.setPath("/");
		cookie.setMaxAge(maxAgeInSeconds);
		cookie.setHttpOnly(true);
		response.addCookie(cookie);
	}
	
	/**
	 * 判断是否ajax请求
	 * spring ajax 返回含有 ResponseBody 或者 RestController注解
	 * @param handlerMethod HandlerMethod
	 * @return 是否ajax请求
	 */
	public static boolean isAjax(HandlerMethod handlerMethod) {
		ResponseBody responseBody = handlerMethod.getMethodAnnotation(ResponseBody.class);
		if (null != responseBody) {
			return true;
		}
		RestController restAnnotation = handlerMethod.getBeanType().getAnnotation(RestController.class);
		if (null != restAnnotation) {
			return true;
		}
		return false;
	}
	
	/**
	 * 获取访问IP地址
	 * 
	 * @param request
	 * @return
	 */
	public static String getIpAddress(HttpServletRequest request){
		String ip = request.getHeader("Cdn-Src-Ip");
		String localIP = "127.0.0.1";
		if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase(localIP) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("x-forwarded-for");
		}
		if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase(localIP) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || ip.equalsIgnoreCase(localIP) || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || (ip.equalsIgnoreCase(localIP))) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || (ip.equalsIgnoreCase(localIP))) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || (ip.equalsIgnoreCase(localIP))) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

}
